package com.inspur.edp.web.formmetadata.i18n.component.strategy.i18nresourcestrategy;
import com.inspur.edp.lcm.metadata.api.entity.I18nResourceItem;
import com.inspur.edp.lcm.metadata.api.entity.I18nResourceItemCollection;
import com.inspur.edp.web.common.utility.StringUtility;
import com.inspur.edp.web.formmetadata.i18n.I18nResourceItemManager;
import com.inspur.edp.web.formmetadata.i18n.component.ComponentUtility;
import com.inspur.edp.web.formmetadata.i18n.component.I18nResourceUtility;
import com.inspur.edp.web.formmetadata.i18n.component.strategy.namestrategy.ComponentNameFactory;
import com.inspur.edp.web.formmetadata.i18n.component.strategy.namestrategy.ComponentNameType;
import com.inspur.edp.web.formmetadata.i18n.component.strategy.namestrategy.IComponentNameStrategy;
import com.inspur.edp.web.formmetadata.i18n.constant.I18nResourceConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class FilterBarResourceStrategy extends AbstractI18nResourceStrategy{
    /**
     * 获取当前组件的名称
     *
     * @param currentComponent
     * @return
     */
    @Override
    protected String getComponentName(HashMap<String, Object> currentComponent) {
        // 读取组件名称
        IComponentNameStrategy componentNameStrategy = ComponentNameFactory.getInstance().creatComponentNameStrategy(ComponentNameType.PRESET_QUERY_SOLUTION_NAME);
        if (componentNameStrategy == null) {
            return null;
        }

        String componentName = componentNameStrategy.getComponentName(currentComponent);

        return componentName;
    }
    /**
     * 获取组件属性中多语资源项集合
     *
     * @param i18nResourceItemBaseId
     * @param currentComponent
     * @return
     */
    @Override
    protected I18nResourceItemCollection extractAttributeI18nResourceItemCollection(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();
        // 提取 resetText 的参数值
        String resetText = "resetText";
        if (currentComponent.containsKey(resetText)&&currentComponent.get(resetText)!=null) {
            // 表示筛选按钮文本为常量显示
            if (currentComponent.get(resetText) instanceof String) {
                String currentComponentId = ComponentUtility.getInstance().getId(currentComponent);
                String filterTextValue = ComponentUtility.getInstance().getValue(currentComponent, resetText);
                String generatedComponentId =  currentComponentId + "/resetText";
                I18nResourceItem i18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId, filterTextValue, "筛选按钮文本");
                this.addInCollection(i18nResourceItemCollection, i18nResourceItem);
            }
        }

        //从fields里提取多语字段
        if (currentComponent.containsKey("fields")){
            List<HashMap<String, Object>> childComponents = ComponentUtility.getInstance().GetFieldsFromContentTemplate(currentComponent);

            String currentComponentId = ComponentUtility.getInstance().getId(currentComponent);
            for (HashMap<String, Object> childComponent : childComponents) {
                String childID = ComponentUtility.getInstance().getValue(childComponent, "id");
                String childName = ComponentUtility.getInstance().getValue(childComponent, "name");
                String generatedComponentId = currentComponentId + "/" + "fields/" + childID;
                I18nResourceItem i18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId,childName, childName);
                this.addInCollection(i18nResourceItemCollection, i18nResourceItem);
                if (((HashMap<String, Object>) childComponent).containsKey("editor")) {
                    HashMap<String, Object> editor = (HashMap<String, Object>) childComponent.get("editor");
                    if (editor.containsKey("data")) {
                        ArrayList<HashMap<String, String>> datas = (ArrayList<HashMap<String, String>>) editor.get("data");
                        for (HashMap<String, String> data : datas) {
                            String code = String.valueOf(data.get("value"));
                            String name = data.get("name");
                            String grandChildGeneratedComponentId = generatedComponentId + "/editor/data/" + code;
                            I18nResourceItem childI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, grandChildGeneratedComponentId, name, name);
                            this.addInCollection(i18nResourceItemCollection, childI18nResourceItem);
                        }
                    }
                    if (editor.containsKey("beginPlaceholder")&&editor.get("beginPlaceholder")!=null) {
                        String beginPlaceholder =(String) editor.get("beginPlaceholder");
                        String grandChildGeneratedComponentId = generatedComponentId + "/editor/beginPlaceholder";
                        I18nResourceItem childI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, grandChildGeneratedComponentId, beginPlaceholder, beginPlaceholder);
                        this.addInCollection(i18nResourceItemCollection, childI18nResourceItem);
                    }
                    if (editor.containsKey("endPlaceholder")&&editor.get("endPlaceholder")!=null) {
                        String endPlaceholder = (String) editor.get("endPlaceholder");
                        String grandChildGeneratedComponentId = generatedComponentId + "/editor/endPlaceholder";
                        I18nResourceItem childI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, grandChildGeneratedComponentId, endPlaceholder, endPlaceholder);
                        this.addInCollection(i18nResourceItemCollection, childI18nResourceItem);
                    }
                    if (editor.containsKey("placeholder")&&editor.get("placeholder")!=null) {
                        String placeholder = (String) editor.get("placeholder");
                        String grandChildGeneratedComponentId = generatedComponentId + "/editor/placeholder";
                        I18nResourceItem childI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, grandChildGeneratedComponentId, placeholder, placeholder);
                        this.addInCollection(i18nResourceItemCollection, childI18nResourceItem);
                    }
                    if (editor.containsKey("dialog")&&editor.get("dialog")!=null) {
                        HashMap<String, String> dialog = (HashMap<String, String>) editor.get("dialog");
                        if (dialog.containsKey("title")&&dialog.get("title")!=null){
                            String title = dialog.get("title");
                            String grandChildGeneratedComponentId = generatedComponentId + "/editor/dialog/title";
                            I18nResourceItem childI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, grandChildGeneratedComponentId, title, title);
                            this.addInCollection(i18nResourceItemCollection, childI18nResourceItem);
                        }
                    }
                }
            }

        }

        // 从fieldConfigs属性中提取多语字段
        return i18nResourceItemCollection;
    }
}
